package top.goldenyear.porpoise.system.permission.service;

import com.easy.query.api.proxy.client.EasyEntityQuery;
import java.util.ArrayList;
import java.util.List;

import com.easy.query.solon.annotation.Db;
import lombok.extern.slf4j.Slf4j;
import cn.hutool.v7.core.collection.CollUtil;
import org.noear.solon.annotation.Component;
import top.goldenyear.porpoise.framework.web.core.model.PageResult;
import top.goldenyear.porpoise.framework.web.core.model.query.QPage;
import top.goldenyear.porpoise.framework.web.core.service.BaseService;
import top.goldenyear.porpoise.system.permission.model.Role;
import top.goldenyear.porpoise.system.permission.model.proxy.RoleProxy;

/**
 * 角色表 Service 服务层
 *
 * @author airhead
 * @since 2024-10-12
 */
@Component
@Slf4j
public class RoleService extends BaseService<Role, RoleProxy> {
  @Db
  private EasyEntityQuery easyEntityQuery;

  public RoleService() {
    super(Role.class, RoleProxy.class);
  }

  public Boolean createRole(Role role) {
    return save(role);
  }

  public Boolean updateRole(Role role) {
    return updateById(role);
  }

  public Role getRole(Integer id) {
    return getById(id);
  }

  public Boolean deleteRole(Integer id) {
    return deleteById(id);
  }

  public List<Role> listRole(Role role) {
    return list(role);
  }

  public PageResult<Role> paginateRole(QPage qPage, Role role) {
    return paginate(qPage.getPageNumber(), qPage.getPageSize(), role);
  }

  public List<Role> listRole(List<String> codes, Integer status) {
    if (CollUtil.isEmpty(codes)) {
      return new ArrayList<>();
    }

    return list(
        roleProxy -> {
          roleProxy.code().in(codes);
          roleProxy.status().eq(status);
        });
  }

  @Override
  protected EasyEntityQuery easyEntityQuery() {
    return easyEntityQuery;
  }
}
